package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.model.DeviceState;
import com.kust.smarthome.web.model.Hygrothermograph;
import com.kust.smarthome.web.model.Notice;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IDeviceStateService;
import com.kust.smarthome.web.service.IHygrothermographService;
import com.kust.smarthome.web.service.INoticeService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/hygrothermograph")
@Api(value = "温湿度计", tags = {"device-tag"})
public class HygrothermographController {

    @Resource
    private IHygrothermographService hygrothermographService;

    @Resource
    private IDeviceStateService deviceStateService;

    @Resource
    private IUserService userService;

    @GetMapping("")
    @ApiOperation(value = "温湿度信息", notes = "通过用户信息获取该用户的温湿度计信息")
    public List<Hygrothermograph> list(Principal principal) {
        User u = userService.getByUsername(principal.getName());
        return hygrothermographService.getByUserId(u.getId());
    }

    @GetMapping("/getById")
    @ApiOperation(value = "getById", notes = "通过id获取对应设备")
    public Hygrothermograph getById(Integer id) {
        return hygrothermographService.getById(id);
    }

    @GetMapping("/update")
    @ApiOperation(value = "温湿度计更新信息", notes = "温湿度计提供本身的id以及对应的content(T:xx;H:xx)温湿度信息")
    public boolean updateById(Integer id,String content,Integer state) {

        Hygrothermograph hygrothermograph = hygrothermographService.getById(id);
        hygrothermograph.setContent(content);
        hygrothermograph.setState(state);

        String[] contents = content.split(";");

        double c = Double.parseDouble(contents[0].split(":")[1]);

        String msg = "";
        if (c >= 40) {
            msg = "温度过高";
        } else if (c <= 16) {
            msg = "温度过低";
        }

        this.warning(hygrothermograph.getUserId(), id, msg);

        hygrothermographService.updateById(hygrothermograph);

        DeviceState deviceState = new DeviceState()
                .setDeviceId(id)
                .setTypeId(7)
                .setIsUpdate(0);

        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id",id)
                .eq("type_id",3);
        deviceStateService.update(deviceState,query);

        return true;
    }

    @PostMapping("/add")
    @Transactional
    @ApiOperation(value = "添加温湿度计", notes = "用户提供id添加温湿度计")
    public List<Hygrothermograph> add(Principal principal, DeviceVo deviceVo) {
        User u = userService.getByUsername(principal.getName());

        List<Hygrothermograph> list = hygrothermographService.getByUserId(u.getId());

        LocalDateTime now = LocalDateTime.now();
        if (list.size() < 3) {
            Hygrothermograph hygrothermograph = new Hygrothermograph()
                    .setId(deviceVo.getId())
                    .setUrl("")
                    .setContent("C:0;H:0")
                    .setState(1)
                    .setCreated(now)
                    .setUserId(u.getId())
                    .setRoomId(deviceVo.getRoomId());
            list.add(hygrothermograph);
            hygrothermographService.insert(hygrothermograph);

            DeviceState deviceState = new DeviceState()
                    .setDeviceId(deviceVo.getId())
                    .setTypeId(7)
                    .setIsUpdate(0);

            deviceStateService.insert(deviceState);
        }
        return list;
    }

    @GetMapping("/deleteById")
    @Transactional
    @ApiOperation(value = "删除温湿度计", notes = "用户提供id删除温湿度计")
    public boolean deleteById(Integer id) {
        Hygrothermograph hygrothermograph = hygrothermographService.getById(id);
        hygrothermograph.setUserId(-1);
        boolean d = hygrothermographService.updateById(hygrothermograph);
        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id", id);
        query.eq("type_id", 7);
        deviceStateService.remove(query);

        return d;
    }

    @Resource
    private INoticeService noticeService;


    /**
     * 此处content格式为: C:30;H:70  第一项为温度,第二项为湿度
     * @param userId 登录之后自带
     * @param id 温湿度计id
     * @param content 温湿度计中产生的信息
     */
    public void warning(Integer userId, Integer id, String content) {

        Notice notice = new Notice()
                .setUserId(userId)
                .setTypeId(7)
                .setDeviceId(id)
                .setContent(content)
                .setCreated(LocalDateTime.now())
                .setState(0);

        noticeService.save(notice);
    }
}
